<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-05-18
 * Time: 15:11
 */

namespace app\api\controller\v1;

use app\api\model\Distribution;
use app\api\model\Register as  RegisterModel;
class Register
{
    /**
     * 获取药品登记记录
     * @validate('PagingParameter')
     */
    public function getRegisterList($page,$limit,$name,$drugs,$disease)
    {
        $where = [];
        if($name) {
            $where[] = ['name','like','%'.$name.'%'];
        }
        if($drugs) {
            $where[] = ['drugs','like','%'.$drugs.'%'];
        }
        if($disease) {
            $where[] = ['disease','like','%'.$disease.'%'];
        }
        $query = RegisterModel::where($where)->order('id desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function ($item){
            if($item['sex']) {
                $item['sex_name'] = '男';
            } else {
                $item['sex_name'] = '女';
            }
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    /**
     * 获取药品登记记录
     */
    public function getRegisterAll($name,$drugs,$disease)
    {
        $where = [];
        if($name) {
            $where[] = ['name','like','%'.$name.'%'];
        }
        if($drugs) {
            $where[] = ['drugs','like','%'.$drugs.'%'];
        }
        if($disease) {
            $where[] = ['disease','like','%'.$disease.'%'];
        }
        return RegisterModel::where($where)->order('id desc')->select()->each(function ($item){
            if($item['sex']) {
                $item['sex_name'] = '男';
            } else {
                $item['sex_name'] = '女';
            }
        })->toArray();
    }

    /**
     * 获取分销返利登记表
     */
    public function getDistributionList($page,$limit,$name,$wx_no,$address)
    {
        $where = [];
        if($name) {
            $where[] = ['name','like','%'.$name.'%'];
        }
        if($wx_no) {
            $where[] = ['wx_no','like','%'.$wx_no.'%'];
        }
        if($address) {
            $where[] = ['address','like','%'.$address.'%'];
        }
        $query = Distribution::where($where)->order('id desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    /**
     * 获取药品登记记录
     */
    public function getDistributionAll($name,$wx_no,$address)
    {
        $where = [];
        if($name) {
            $where[] = ['name','like','%'.$name.'%'];
        }
        if($wx_no) {
            $where[] = ['wx_no','like','%'.$wx_no.'%'];
        }
        if($address) {
            $where[] = ['address','like','%'.$address.'%'];
        }
        return Distribution::where($where)->order('id desc')->select()->toArray();
    }
}